package com.juneng.utils.wxpay;

import com.github.wxpay.sdk.WXPayUtil;
import com.juneng.utils.wechat.WeChatConfig;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author chang
 * @create 2021/06/09 16:48
 * @description 微信支付配置工具类
 */
public class MyWxPayUtil {

    //api key
    public final static String api_key = "fas654af4f1as1df3a2df46asd511v3a";

    //支付回调地址
    public final static String notify_url = "http://manniuwechat.lzcoke.com/payment/notify/wx/pay";

    //退款回调地址
    public final static String refund_notify_url = "http://manniuwechat.lzcoke.com/payment/notify/wx/refund";

    /**
     * 获取10位随机数
     * @return nonceStr
     */
    public static String getNonceStr() {
        return UUID.randomUUID().toString().replace("-", "").substring(0, 10);
    }

    /**
     * 获取客户端真实IP
     * @return ip
     */
    public static String getRemoteHost() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }


    /**
     * 提取统一下单的返回结果，转换成调用微信支付所需要的参数
     * APP端
     * @param resp
     * @return
     */
    public static Map<String, String> respToResultApp(Map<String, String> resp) {
        Map<String, String> map = new HashMap<>();
        map.put("appId", resp.get("appid"));
        map.put("timeStamp", (System.currentTimeMillis() / 1000) + "");
        map.put("nonceStr", WXPayUtil.generateNonceStr());
        map.put("package", "prepay_id="+resp.get("prepay_id"));
        map.put("signType", "MD5");
        String sign = null;
        try {
            sign = WXPayUtil.generateSignature(map, WeChatConfig.APP_KEY);
        } catch (Exception e) {
            System.out.println("获取sign失败！");
        }
        map.put("paySign", sign);
        return map;
    }



    /**
     * 提取统一下单的返回结果，转换成调用微信支付所需要的参数
     * 小程序端
     * @param resp
     * @return
     */
//    public static Map<String, String> respToResultMini(Map<String, String> resp) {
//        Map<String, String> map = new HashMap<String, String>();
//        String packagepam = "prepay_id=" + resp.get("prepay_id") + "";
//        map.put("appId", resp.get("appid"));
//        map.put("timeStamp", System.currentTimeMillis() + "");
//        map.put("nonceStr", WXPayUtil.generateNonceStr());
//        map.put("package", packagepam);
//        map.put("signType", "MD5");
//        String paySign = null;
//        try {
//            paySign = WXPayUtil.generateSignature(map, WxPayConfig.api_key);
//        } catch (Exception e) {
//            System.out.println("获取paySign失败！");
//        }
//        map.put("paySign", paySign);
//        return map;
//    }

}
